#ifndef CUSTOM_UNLIT_PASS_INCLUDED // 避免重复引入
#define CUSTOM_UNLIT_PASS_INCLUDED

#include "../ShaderLibrary/Common.hlsl"

// 适应批处理
TEXTURE2D(_BaseMap);
SAMPLER(sampler_BaseMap);

UNITY_INSTANCING_BUFFER_START(UnityPerMaterial)
    UNITY_DEFINE_INSTANCED_PROP(float4, _BaseMap_ST)
    UNITY_DEFINE_INSTANCED_PROP(float4, _BaseColor)
    UNITY_DEFINE_INSTANCED_PROP(float, _Cutoff)
UNITY_INSTANCING_BUFFER_END(UnityPerMaterial)

// 适用GPU实例化的输入结构
struct Attributes {
    float3 positionOS : POSITION;
    float2 baseUV : TEXCOORD0; // uv坐标
    UNITY_VERTEX_INPUT_INSTANCE_ID // 对象索引
};

// 适用GPU实例化的输出结构
struct Varyings {
    float4 positionCS : SV_POSITION;
    float2 baseUV : VAR_BASE_UV;
    UNITY_VERTEX_INPUT_INSTANCE_ID
};

// 顶点着色器
Varyings UnlitPassVertex (Attributes input) // 输入模型空间坐标，返回齐次裁剪空间坐标
{
    Varyings output;
    UNITY_SETUP_INSTANCE_ID(input); // 提取对象索引
    UNITY_TRANSFER_INSTANCE_ID(input, output);
    float3 positionWS = TransformObjectToWorld(input.positionOS); // 模型空间 -> 世界空间
    output.positionCS = TransformWorldToHClip(positionWS);
    
    float4 baseST = UNITY_ACCESS_INSTANCED_PROP(UnityPerMaterial, _BaseMap_ST); // 纹理采样
    output.baseUV = input.baseUV * baseST.xy + baseST.zw; // 位移缩放
    return output;
}

// 片元着色器 
float4 UnlitPassFragment (Varyings input) : SV_TARGET // 返回颜色
{
    UNITY_SETUP_INSTANCE_ID(input);
    float4 baseMap = SAMPLE_TEXTURE2D(_BaseMap, sampler_BaseMap, input.baseUV); // 纹理
    float4 baseColor = UNITY_ACCESS_INSTANCED_PROP(UnityPerMaterial, _BaseColor); // 颜色
    float4 base = baseMap * baseColor;
    
    #if defined(_CLIPPING)
    clip(base.a - UNITY_ACCESS_INSTANCED_PROP(UnityPerMaterial, _Cutoff)); // alpha裁剪
    #endif
    return base;
}
#endif